package com.zmn.oms.task.job.order;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.order.DuplicateOrderBService;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.dto.work.modify.must.ZsCancelDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.normal.business.interfaces.work.ZsNormalWorkBService;
import com.zmn.pay.common.constant.PayConsts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

@Slf4j
@LTS
public class OrderPayTimeoutCheckJob {

    @Autowired
    OrderWorkService orderWorkService;
    @Autowired
    ZsNormalWorkBService zsNormalWorkBService;
    @Autowired
    DuplicateOrderBService duplicateOrderBService;

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_CHANNEL_PAY_TIMEOUT_CANCEL)
    public Result payTimeoutCheck(Job job) {

        log.info("#order#paytimeoutcheck 执行：" + job);

        String orderIdStr = job.getParam("orderId");
        if (!NumberUtil.isNumber(orderIdStr)) {
            return new Result(Action.EXECUTE_FAILED, "订单号错误!");
        }

        log.info("#order#paytimeoutcheck {} 执行超时取消", orderIdStr);
        Long orderId = Long.valueOf(orderIdStr);
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, orderId);
        if (orderWork == null) {
            log.error("#order#paytimeoutcheck 订单不存在");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        if (orderWork.getStatus() >= OrderStatusConsts.WORK_STATUS_COMPLETE) {
            log.error("#order#paytimeoutcheck 订单已完成，不取消");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        if (orderWork.getResultStatus() == OrderStatusConsts.WORK_RESULT_DISCARD || orderWork.getResultStatus() == OrderStatusConsts.WORK_RESULT_FAIL) {
            log.error("#order#paytimeoutcheck 订单已失败，不取消");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        boolean existChannelPrepayAmount = !NumberUtil.isNullOrZero(orderWork.getChannelPrepayAmount());
        boolean isPayed = Objects.equals(orderWork.getChannelPrepayStatus(), PayConsts.PAY_STATUS_DONE);

        if (existChannelPrepayAmount && isPayed) {
            log.info("#order#paytimeoutcheck 渠道预付款已经支付无需取消");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        boolean existPrepayAmount = !NumberUtil.isNullOrZero(orderWork.getPrepayAmount());
        boolean isPrepayPayed = Objects.equals(orderWork.getPrepayStatus(), PayConsts.PAY_STATUS_DONE);
        if (existPrepayAmount && isPrepayPayed) {
            log.info("#order#paytimeoutcheck 平台预付款已经支付无需取消");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        ZsCancelDTO zsCancelDTO = new ZsCancelDTO();
        zsCancelDTO.setPayTimeout(GlobalConsts.YES);
        zsCancelDTO.setOrderId(orderId);
        zsCancelDTO.setWorkId(orderId);
        zsCancelDTO.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
        zsCancelDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
        zsCancelDTO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
        zsCancelDTO.setContent("用户超时未支付");

        try {
            zsNormalWorkBService.saveCancel(zsCancelDTO);
        } catch (OmsBaseException e) {
            log.error("#order#paytimeoutcheck 取消异常 {} {}", e.getMessage(), e);
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        } catch (Exception e) {
            log.error("#order#paytimeoutcheck 取消异常2 {} {}", e.getMessage(), e);
        }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
